package dk.fungps;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;

public class CenterActivity extends Activity {
	
	private static final float DISTANCE_TO_MOVE = 20.0F;
	
	//SADAFG
	
	private GpsService mGpsService;
	private Location mStartPoint;
	private String mTitle;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // retrieve intent arguments
        mTitle = getIntent().getStringExtra("position");
        if(mTitle == null) {
        	mTitle = "Center";
        }
        mStartPoint = (Location) getIntent().getParcelableExtra("start");
        
        setTitle(mTitle);
        
        // bind to our service
        Intent i = new Intent(CenterActivity.this, GpsService.class);
        bindService(i, mServiceConnection, BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	unbindService(mServiceConnection);
    }
    
    private ServiceConnection mServiceConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName c) {
			
		}

		// will be called when the connection to the service has been establish
		@Override
		public void onServiceConnected(ComponentName c, IBinder b) {
			// get instance of GpsService so we can call methods on it (we don't)
			GpsService.LocalBinder binder = (GpsService.LocalBinder)b;
			mGpsService = binder.bindActivity(mHandler); 
		}
	};
    
	// Handler for the GUI thread owning this activity
    private Handler mHandler = new Handler() {
    	@Override
    	public void handleMessage(android.os.Message msg) {
    		Location l = (Location)msg.obj;
    		
    		if(mStartPoint == null) {
    			mStartPoint = l;
    			Toast.makeText(CenterActivity.this, "Received start point!", Toast.LENGTH_LONG).show();
    		}
    		
    		// if we have moved far enough
    		if(mStartPoint != null && mStartPoint.distanceTo(l) > DISTANCE_TO_MOVE) {
    			
    			// create intent to start new activity
    			Intent i = new Intent(CenterActivity.this, CenterActivity.class);
    			i.putExtra("start", l);
    			
    			// find the bearing
    			float bearing = mStartPoint.bearingTo(l);
    			
    			// find what title to use
    			if(mTitle.equals("Center")) {
    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "North");
    					
    				} else if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "East");
    					
    				} else if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "South");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "West");
    					
    				} 
    			} else if(mTitle.equals("NorthWest")) {
    				
    				if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "North");
					
    				} else if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "West");
					
    				}
    			} else if(mTitle.equals("North")) {
    				
    				if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "NorthEast");
    					
    				} else if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "Center");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "NorthWest");
    					
    				} 
    			} else if(mTitle.equals("NorthEast")) {
    				
    				if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "East");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "North");
    					
    				} 
    			} else if(mTitle.equals("West")) {
    				
    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "NorthWest");
    					
    				} else if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "Center");
    					
    				} else if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "SouthWest");
    					
    				}
    			} else if(mTitle.equals("East")) {

    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "NorthEast");
    					
    				} else if(bearing > 135.0F || bearing < -135.0F) {
    					i.putExtra("position", "SouthEast");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "Center");
    					
    				}
    			} else if(mTitle.equals("SouthWest")) {
    				
    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "West");
    					
    				} else if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "South");
    					
    				}
    			} else if(mTitle.equals("South")) {
    				
    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "Center");
    					
    				} else if(bearing > 45.0F && bearing < 135.0F) {
    					i.putExtra("position", "SouthEast");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "SouthWest");
    					
    				} 
    			} else if(mTitle.equals("SouthEast")) {
    				if(bearing > -45.0F && bearing < 45.0F) {
    					i.putExtra("position", "East");
    					
    				} else if(bearing > -135.0F && bearing < -45.0F) {
    					i.putExtra("position", "South");
    					
    				} 
    			} else {
    				Toast.makeText(CenterActivity.this, "Failure", Toast.LENGTH_LONG).show();
    			}
    			
    			// start new activity
    			startActivity(i);
    		}
    	}
    };
}
